<template>
  <div class="login-container">
    <div class="login-box">
      <div class="title">Vue3 Admin</div>
      <el-form 
        ref="formRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="Lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            :loading="loading"
            type="primary" 
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { FormInstance } from 'element-plus'

const router = useRouter()
const formRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = ref({
  username: 'admin',
  password: '123456'
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!formRef.value) return
  
  try {
    loading.value = true
    await formRef.value.validate()
    
    // TODO: 调用登录接口
    localStorage.setItem('token', 'test-token')
    localStorage.setItem('userInfo', JSON.stringify({
      id: 1,
      username: loginForm.value.username,
      nickname: 'Admin',
      role: 'admin',
      avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
    }))
    
    ElMessage.success('登录成功')
    router.push('/')
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background-color: #2d3a4b;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .login-box {
    width: 400px;
    padding: 20px 35px;
    
    .title {
      font-size: 26px;
      color: #eee;
      text-align: center;
      margin: 0 auto 30px;
    }
    
    .login-form {
      .el-input {
        background: transparent;
        
        :deep(.el-input__wrapper) {
          background-color: rgba(0, 0, 0, 0.1);
          box-shadow: none;
          border: 1px solid rgba(255, 255, 255, 0.1);
          
          &.is-focus {
            border-color: #409EFF;
          }
          
          .el-input__inner {
            color: #fff;
            
            &::placeholder {
              color: rgba(255, 255, 255, 0.5);
            }
          }
          
          .el-input__prefix-icon {
            color: #889aa4;
          }
        }
      }
      
      .login-button {
        width: 100%;
      }
    }
  }
}
</style> 